//
//  NinePicsCell.m
//  XRmenu-HD
//
//  Created by Gao Yan on 11-12-3.
//  Copyright 2011年 home user. All rights reserved.
//

#import "NinePicsCell.h"

@implementation NinePicsCell
#pragma mark 业务实现展示菜品内容
-(void)configItem:(NSInteger)rowIndex
{
    PluPlu *onePluPlu=[APPSESSION.pluPlus objectAtIndex:rowIndex];
    if (onePluPlu.rsToDishOrder !=nil) {
        [self setDishSelected:YES];
    }
    else{
        [self setDishSelected:NO];
    }
	if ([self viewWithTag:kTagOfStartLabel]) {//设置菜名
		UILabel *nameLabl = (UILabel*)[self viewWithTag:kTagOfStartLabel];
		if (nameLabl) {
            NSString *tempName=JsonFiled(onePluPlu.ppNames);
            if ([tempName length]>=9) {
                tempName=[tempName substringToIndex:9];
            }
			[nameLabl setText:tempName];
		}
	}
	if ([self viewWithTag:kTagOfStartLabel+1]) {//设置价格单位
		UILabel *priceLbl = (UILabel*)[self viewWithTag:kTagOfStartLabel+1];
		if (priceLbl) {
            NSString* unit=JsonFiled(onePluPlu.ppUnits);
            NSString* price  = [NSString stringWithFormat:@"%@%0.2f",[APPSESSION.allconfig getStringValueAtPath:@"System Config/currency"],[onePluPlu.ppPrice floatValue]];
			[priceLbl setText:[NSString stringWithFormat:@"%@/%@",price,unit]];
		}
	}
	
    if ([self viewWithTag:kTagOfStartImgV]) {//设置图片
        UIImageView *imgview = (UIImageView*)[self viewWithTag:kTagOfStartImgV];
        if (imgview) {
            XRmenuError* error = nil;
            UIImage *img=getDishGridImg(onePluPlu.ppPluPluId,error);//菜品图片
            [imgview setImage:img];
        }
    }
    
    if ([self viewWithTag:kTagOfStartBT]) {
        UIButton *btntemp =(UIButton *) [self viewWithTag:kTagOfStartBT];             
        [btntemp.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20.0f]];
        [btntemp setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btntemp setBackgroundImage:[UIImage imageWithContentsOfFile:[XRmenuPublic getGeneralSkinImg:@"bt_detail.png"]] forState:UIControlStateNormal];
        [btntemp setTitle:loc(@"详细") forState:UIControlStateNormal];
    }
    if ([self viewWithTag:kTagOfStartBT+1]) {
        UIButton *btntemp =(UIButton *) [self viewWithTag:kTagOfStartBT+1]; 
        [btntemp.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20.0f]];
        [btntemp setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btntemp setBackgroundImage:[UIImage imageWithContentsOfFile:[XRmenuPublic getGeneralSkinImg:@"bt_order.png"]] forState:UIControlStateNormal];
        [btntemp setTitle:loc(@"点单") forState:UIControlStateNormal];
    }
    //设置当前cell是否被选中
    [self setSelectedCell:NO];
}
//该菜品是否已点
-(void) setDishSelected:(BOOL) isSelected{
    if ([self viewWithTag:kTagOfStartImgV+2]) {
        [self viewWithTag:kTagOfStartImgV+2].hidden=!isSelected;
    }
}

//当前是否被选中
-(void) setSelectedCell:(BOOL)selected{
    DebugLog(@"selected");
    [self viewWithTag:kTagOfStartImgV+3].hidden=!selected;
    [self viewWithTag:kTagOfStartBT].hidden=!selected;
    [self viewWithTag:kTagOfStartBT+1].hidden=!selected;
}

#pragma mark 框架继承部分
-(void)buttonTapped:(id)sender
{  
	if (self.delegate) {
		[self.delegate multiColTableCellTapped:self];
	}
} 

#pragma mark 重载button处理方法
-(void) buttonClicked:(id)sender{
	NSInteger bid = [sender tag];
	if (bid == (kTagOfStartBT + 0)) {  
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        postN(kMessage_RootView_PhotoPopoverViewDisplay);
	}
    else if (bid == (kTagOfStartBT + 1)) {
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        //1.插入点单数据
        XRmenuError *outErr=nil;
        [XRmenuCoreData insertNewDishOrder:APPSESSION.currentPluPlu Error:&outErr];
        //2.界面上更新我的点单数量
        postN(kMessage_MainView_MyOrderNumberChanged);
        //3.设置已点标志
        [self setDishSelected:YES];
	}
}
@end
